<template>
  <div class="rank" ref='rank'>
    <Scroll class='toplist' :data='topList' ref='toplist'>
      <ul>
        <li v-for='item in topList' class='item' @click='itemClick(item)'>
          <div class='icon'>
            <img width='100' height='100' v-lazy='item.coverImgUrl' alt=''>
          </div>
          <ul class='songlist'>
            <li v-for='(song,index) in item.tracks' class='song'>
              <span>{{index+1}}</span>
              <span>{{song.first}}-{{song.second}}</span>
            </li>
          </ul>
        </li>
      </ul>
    </Scroll>
    <div class='loading-container' v-show='!topList.length'>
      <loading></loading>
    </div>
    <router-view></router-view>
  </div>
</template>

<script>
  import { getToplist } from 'api'
  import Scroll from 'base/scroll/scroll'
  import Loading from 'base/loading/loading'
  import { playlistMixin } from 'common/js/mixin'
  import { mapMutations } from 'vuex'
  import { SET_TOPLIST } from '../../store/mutation-types'

  export default {
    mixins: [playlistMixin],
    components: {
      Scroll,
      Loading
    },
    data() {
      return {
        topList: []
      }
    },
    props: {},
    methods: {
      handlePlaylist(newVal) {
        const bottom = newVal.length > 0 ? '60px' : '0'
        this.$refs.rank.style.bottom = bottom
        this.$refs.toplist.refresh()
      },
      itemClick(item) {
        this.$router.push({
          path: `/rank/${item.id}`
        })
        this.setTopList(item)
      },
      _getToplist() {
        getToplist().then((res) => {
          this.topList = res.data.list
          // this.topList = res.data.list.filter((item)=>{
          //   return item.tracks.length > 0
          // })
        })
      },
      ...mapMutations({
        setTopList: SET_TOPLIST
      })
    },
    created() {
      this._getToplist()
    },
    mounted() {
    },
    destroyed() {
    }
  }
</script>

<style lang="stylus" scoped>
  @import "~common/stylus/variable"
  @import "~common/stylus/mixin"

  .rank
    position: fixed
    width: 100%
    top: 88px
    bottom: 0
    .toplist
      height: 100%
      overflow: hidden
      .item
        display: flex
        margin: 0 20px
        padding-top: 20px
        height: 100px
        &:last-child
          padding-bottom: 20px
        .icon
          flex: 0 0 100px
          width: 100px
          height: 100px
        .songlist
          flex: 1
          display: flex
          flex-direction: column
          justify-content: center
          padding: 0 15px
          height: 100px
          overflow: hidden
          background: $color-highlight-background
          color: $color-text-d
          font-size: $font-size-small
          .song
            no-wrap()
            line-height: 26px
            span:first-child
              margin-right 2px
      .loading-container
        position: absolute
        width: 100%
        top: 50%
        transform: translateY(-50%)
</style>
